class MapApplication {
    constructor() {
        this.map = null;
        this.parkData = null;
    }
    initMap(mapID, listSpaceByArea) {
        if (typeof esmap === 'undefined') {
            console.error('ESMap library not loaded');
            return;
        }
        try {
            console.log('Initializing ESMap with ID:', mapID);
            const container = document.getElementById('map-container');
            if (!container) {
                console.error('Map container not found!');
                return;
            }
            
            const mapOptions = {
                container: container,
                mapDataSrc: '../static/esmap/data/',
                token: 'escope'
            };
            
            if (typeof esmap.MapMode !== 'undefined' && esmap.MapMode.Building) {
                mapOptions.mode = esmap.MapMode.Building;
            } else if (typeof esmap.ESBuildingType !== 'undefined') {
                mapOptions.buildingType = esmap.ESBuildingType.BUILDING;
            }
            
            console.log('Map options:', mapOptions);
            
            // 创建地图实例
            this.map = new esmap.ESMap(mapOptions);
            
            // 添加楼层控制
            this.addFloorControl();
            this.getParkingData(listSpaceByArea, 1)
            // 延迟打开地图
            setTimeout(() => {
                if (this.map && typeof this.map.openMapById === 'function') {
                    console.log('Opening map with ID:', mapID);
                    this.map.openMapById(mapID);
                }
            }, 100);
        } catch (error) {
            console.error('Error initializing map:', error);
            console.error('Error stack:', error.stack);
        }
    }

    // 获取停车位数据
    getParkingData(listSpaceByArea, scAreaId) {

        listSpaceByArea({
            scAreaId:scAreaId
        }).then(response => {
            this.map.on('loadComplete', () => {
                this.parkData = response.rows;
                this.callLoadData(response.rows);
            });
        });
    }

    // 渲染停车位状态
    callLoadData(res) {
        var id = [];
        var mydata=new Map();
        var parkData = res;
        //1.解析数据,将数据按每层进行整理
        var total =  0;
        for (var i = 0, len = parkData.length; i < len; ++i) { 
            var m = parkData[i];
            var d=mydata.get((m.scParkingLevelId % 2 % 2 == 0) ? 2 : 1);
            
            if(d==null)
            {
                d = {"idlist":[[],[],[],[],[]]};
            }
            total++;
            
            d.idlist[m.scSpaceStatus].push(m.scSpaceName);
            mydata.set((m.scParkingLevelId % 2 % 2 == 0) ? 2 : 1,d);
        }
        // var showtext="";
        var color = ["#00FF00", "#0000FF", "#FF0000", "#808080", "#800080"];
        //2.更新车位颜色
        for (var i = 1; i < mydata.size + 1; i++) {
            var d1 = mydata.get(i);
            // var fnum = d1[0];
            // var d = d1[1];
            // console.log(d1);
            for(var j=0;j<color.length;j++)
            {
                // console.log('参数:', {
                //     name: d1.idlist[j], // 应为数组或字符串
                //     fnum: fnum,
                //     color: color[j]
                // });
                //调用批量修改颜色接口来修改
                
                this.map.changeModelColor({
                    name:d1.idlist[j],
                    color: color[j]
                });
                
            }
        }               
    }

    // 新增：添加楼层控制⽅法
    addFloorControl() {
        try {
            if (!this.map) {
                console.error('Map instance not initialized');
                return;
            }
            // 楼层控制控件配置参数
            const ctlOpt = new esmap.ESControlOptions({
                position: esmap.ESControlPositon.RIGHT_TOP,
            });
            // 监听地图加载完成事件
            this.map.on('loadComplete', () => {
                // 创建楼层控件
                const floorControl = new esmap.ESScrollFloorsControl(this.map, ctlOpt);
                console.log('Floor control added successfully');
            });
        } catch (error) {
            console.error('Error adding floor control:', error);
            console.error('Error stack:', error.stack);
        }
    }

    /**
     * 销毁三维场景
     */
    destroyMap() {
        try {
            if (this.map) {
                if (typeof this.map.destroy === 'function') {
                    this.map.destroy();
                } else if (typeof this.map.dumpScene === 'function') {
                    // 兼容旧版本
                    this.map.dumpScene();
                }
            this.map = null;
            console.log('Map destroyed');
            }
        } catch (error) {
            console.error('Error destroying map:', error);
        }
    }
}

export default new MapApplication();